package com.manage.project.biz.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.manage.project.biz.domain.*;
import com.manage.project.biz.service.*;
import com.manage.project.system.domain.SysNotice;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.web.page.TableDataInfo;

//import com.ruoyi.common.annotation.Log;
//import com.ruoyi.common.core.controller.BaseController;
//import com.ruoyi.common.core.domain.AjaxResult;
//import com.ruoyi.common.enums.BusinessType;

//import com.ruoyi.common.utils.poi.ExcelUtil;
//import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 专项检查Controller
 *
 * @author ycd
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/biz/specialinspection")
public class CivilEnginneeringSpecialInspectionController extends BaseController
{
    @Autowired
    private ICivilEnginneeringSpecialInspectionService civilEnginneeringSpecialInspectionService;
    @Autowired
    private IBizGongluService roadInfoService;
    @Autowired
    private IBizSuidaoService iBizSuidaoService;
    @Autowired
    private ICivilEnginneeringFacilityService iCivilEnginneeringFacilityService;
    @Autowired
    private ICivilEnginneeringSpecialEvaluateService iCivilEnginneeringSpecialEvaluateService;


    /**
     * 查询专项检查列表
     */
    @PreAuthorize("@ss.hasPermi('biz:specialinspection:list')")
    @GetMapping("/list")
    public TableDataInfo list(CivilEnginneeringSpecialInspection civilEnginneeringSpecialInspection)
    {
        startPage();
        List<CivilEnginneeringSpecialInspection> list = civilEnginneeringSpecialInspectionService.selectCivilEnginneeringSpecialInspectionList(civilEnginneeringSpecialInspection);
        return getDataTable(list);
    }

    /**
     * 导出专项检查列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:specialinspection:export')")
//    @Log(title = "专项检查", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, CivilEnginneeringSpecialInspection civilEnginneeringSpecialInspection) throws IOException {
//        List<CivilEnginneeringSpecialInspection> list = civilEnginneeringSpecialInspectionService.selectCivilEnginneeringSpecialInspectionList(civilEnginneeringSpecialInspection);
//        ExcelUtil<CivilEnginneeringSpecialInspection> util = new ExcelUtil<CivilEnginneeringSpecialInspection>(CivilEnginneeringSpecialInspection.class);
//        util.exportExcel(response, list, "专项检查数据");
//    }

    @Log(title = "专项检查", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export( CivilEnginneeringSpecialInspection civilEnginneeringSpecialInspection) throws IOException {
        List<CivilEnginneeringSpecialInspection> list = civilEnginneeringSpecialInspectionService.selectCivilEnginneeringSpecialInspectionList(civilEnginneeringSpecialInspection);
        ExcelUtil<CivilEnginneeringSpecialInspection> util = new ExcelUtil<CivilEnginneeringSpecialInspection>(CivilEnginneeringSpecialInspection.class);
        return util.exportExcel(list, "专项检查数据");
    }
    /**
     * 获取专项检查详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:specialinspection:query')")
    @GetMapping(value = "/{specialInspectionId}")
    public AjaxResult getInfo(@PathVariable("specialInspectionId") Long specialInspectionId)
    {
        return AjaxResult.success(civilEnginneeringSpecialInspectionService.selectCivilEnginneeringSpecialInspectionBySpecialInspectionId(specialInspectionId));
    }

    /**
     * 新增专项检查
     */
    @PreAuthorize("@ss.hasPermi('biz:specialinspection:add')")
    @Log(title = "专项检查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CivilEnginneeringSpecialInspection civilEnginneeringSpecialInspection)
    {
        String roadId=civilEnginneeringSpecialInspection.getRoadId();
        String roadName=civilEnginneeringSpecialInspection.getRoadName();
        String tunnelName=civilEnginneeringSpecialInspection.getTunnelName();
        String tunnelId=civilEnginneeringSpecialInspection.getTunnelId();
//        String positionDescribe=civilEnginneeringSpecialInspection.getPositionDescribe();
        String maintenanceOrganization=civilEnginneeringSpecialInspection.getMaintenanceOrganization();
        String inspection=civilEnginneeringSpecialInspection.getInspection();
        String facility=civilEnginneeringSpecialInspection.getFacilityName();
        String mileageNumber=civilEnginneeringSpecialInspection.getMileageNumber();
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }
        CivilEnginneeringFacility civilEnginneeringFacility=iCivilEnginneeringFacilityService.getByNameAndMileageNumber(tunnelName,facility,mileageNumber);
        if(civilEnginneeringFacility==null){
            return new AjaxResult().error(tunnelName+mileageNumber+"里程桩号位置未安装"+facility+",请联系管理员");
        }
//        自动生成专项检查评价任务
        CivilEnginneeringSpecialEvaluate civilEnginneeringSpecialEvaluate =new CivilEnginneeringSpecialEvaluate();
        civilEnginneeringSpecialEvaluate.setRoadId(roadId);
        civilEnginneeringSpecialEvaluate.setRoadName(roadName);
        civilEnginneeringSpecialEvaluate.setTunnelId(tunnelId);
        civilEnginneeringSpecialEvaluate.setTunnelName(tunnelName);
        civilEnginneeringSpecialEvaluate.setFacilityName(facility);
        civilEnginneeringSpecialEvaluate.setMaintenanceOrganization(maintenanceOrganization);
        civilEnginneeringSpecialEvaluate.setInspection(inspection);
        iCivilEnginneeringSpecialEvaluateService.insertCivilEnginneeringSpecialEvaluate(civilEnginneeringSpecialEvaluate);

        BizGonglu roadInfo=roadInfoService.checkByRoadName(roadName);
        if(roadInfo==null) {
            return new AjaxResult().error("当前道路不存在，请输入正确的道路名称");
        }else {
            return toAjax(civilEnginneeringSpecialInspectionService.insertCivilEnginneeringSpecialInspection(civilEnginneeringSpecialInspection));
        }
    }

    /**
     * 修改专项检查
     */
    @PreAuthorize("@ss.hasPermi('biz:specialinspection:edit')")
    @Log(title = "专项检查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CivilEnginneeringSpecialInspection civilEnginneeringSpecialInspection)
    {
        Long SpecialInspectionId=civilEnginneeringSpecialInspection.getSpecialInspectionId();
        String roadId=civilEnginneeringSpecialInspection.getRoadId();
        String maintenanceOrganization=civilEnginneeringSpecialInspection.getMaintenanceOrganization();
        String inspection=civilEnginneeringSpecialInspection.getInspection();
        String roadName=civilEnginneeringSpecialInspection.getRoadName();
        String tunnelName=civilEnginneeringSpecialInspection.getTunnelName();
        String tunnelId=civilEnginneeringSpecialInspection.getTunnelId();
        String facility=civilEnginneeringSpecialInspection.getFacilityName();
        String mileageNumber=civilEnginneeringSpecialInspection.getMileageNumber();
        BizSuidao bizSuidao= iBizSuidaoService.getByTunnelNameIdAndRoadName(roadName,tunnelName,tunnelId);
        if(bizSuidao==null){
            return new AjaxResult().error("当前公路名称、隧道名称和隧道编码不对应，请核对后重新输入");
        }

        CivilEnginneeringFacility civilEnginneeringFacility=iCivilEnginneeringFacilityService.getByNameAndMileageNumber(tunnelName,facility,mileageNumber);
        if(civilEnginneeringFacility==null){
            return new AjaxResult().error(tunnelName+mileageNumber+"里程桩号位置未安装"+facility+",请联系管理员");
        }
        //        修改生成专项检查评价任务,获得和检查id相同的评价表
        CivilEnginneeringSpecialEvaluate civilEnginneeringSpecialEvaluate =iCivilEnginneeringSpecialEvaluateService.selectCivilEnginneeringSpecialEvaluateBySpecialEvaluateId(SpecialInspectionId);
            civilEnginneeringSpecialEvaluate.setRoadId(roadId);
            civilEnginneeringSpecialEvaluate.setRoadName(roadName);
            civilEnginneeringSpecialEvaluate.setTunnelId(tunnelId);
            civilEnginneeringSpecialEvaluate.setTunnelName(tunnelName);
            civilEnginneeringSpecialEvaluate.setFacilityName(facility);
            civilEnginneeringSpecialEvaluate.setMaintenanceOrganization(maintenanceOrganization);
            civilEnginneeringSpecialEvaluate.setInspection(inspection);
        iCivilEnginneeringSpecialEvaluateService.updateCivilEnginneeringSpecialEvaluate(civilEnginneeringSpecialEvaluate);
        BizGonglu roadInfo=roadInfoService.checkByRoadName(roadName);
        if(roadInfo==null) {
            return new AjaxResult().error("当前道路不存在，请输入正确的道路名称");
        }else {
            return toAjax(civilEnginneeringSpecialInspectionService.updateCivilEnginneeringSpecialInspection(civilEnginneeringSpecialInspection));
        }
    }

    /**
     * 删除专项检查
     */
    @PreAuthorize("@ss.hasPermi('biz:specialinspection:remove')")
    @Log(title = "专项检查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{specialInspectionIds}")
    public AjaxResult remove(@PathVariable Long[] specialInspectionIds)
    {
        //        修改生成专项检查评价任务,获得和检查id相同的评价表
        Long[] specialEvaluateIds=specialInspectionIds;
        iCivilEnginneeringSpecialEvaluateService.deleteCivilEnginneeringSpecialEvaluateBySpecialEvaluateIds(specialEvaluateIds);
        return toAjax(civilEnginneeringSpecialInspectionService.deleteCivilEnginneeringSpecialInspectionBySpecialInspectionIds(specialInspectionIds));
    }
}
